<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header, .footer, .pagination, .content {
      width: 1200px;
      margin: 0 auto;
    }

    .header, .footer {
      height: 100px;
      background-color: skyblue;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 50px;
      color: #fff;
    }

    .pagination {
      height: 45px;
      margin: 10px auto;
      border: 1px solid #333;
    }

    .content {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }

    .content > li {
      list-style: none;
      width: 290px;
      height: 450px;
      border: 1px solid #333;
      margin-bottom: 10px;
    }

    .content > li > img {
      width: 100%;
      display: block;
    }
  </style>
</head>
<body>

  <div class="header">头部</div>
  <div class="pagination">
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
  </div>
  <ul class="content">

  </ul>
  <div class="footer">底部</div>
  <script>
    // 1.请求当前页的数据 
    // 2.渲染当前页的数据到页面上
    // 3.打开页面渲染第一页 
    // 4.上一页 请求上一页的数据 渲染 
    // 5.下一页 请求下一页的数据渲染 

    // 请求当前页的数据 ajax
    let contentBox  = document.querySelector('.content')
    let current = 1;
    let pagesize = 8;
    const prevBtn = 
    getList()
    function getList(){
        const xhr = new XMLHttpRequest();

        xhr.open('get',`http://localhost:8080/goods/list?current=${current}&pagesize=${pagesize}`,true);

        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    const res = JSON.parse(xhr.responseText).list;
                    bindHTML(res);
                }
            }
        }
        xhr.send();
    }

    // 渲染当前页的数据 
    function bindHTML(data){
        contentBox.innerHTML = data.reduce((prev,item)=>{
            return prev +`
                <li>
                    <img src=${item.img_big_logo}>
                    <p>${item.title}</p>
                    <p>${item.price}元</p>

                </li>
            
            `
        },'')
    }

    // 上一页
    document.querySelector('.prev').onclick = function(){
        if(current === 1){
            return 
        }
        current--;
        getList();
    }

    // 下一页
    document.querySelector('.next').onclick = function(){
        current++;
        getList();
    }

  </script>
</body>
</html>